Level3 = Level1.extend({
	init: function(context, images, enemyShotAudio) {
		this._super(context, images, enemyShotAudio);
		
		this.firstShootFromEnemy = 1000;
		this.secondShootFromEnemy = 2000;
		var d = new Date();
		this.startLevel3 = d.getTime();
		this.initHelicopter = 8000;
		this.count = 0;
		this.countShootH =1;
		this.shootToHelicopter = 0;
		this.deadHelicopter = false;
		this.frameDeadHelicopter = 0;
		this.flagStartDeadAnimate = false;
	},
	
	generateRandomEnemies: function() {
		var self = this;
		
	    var randomNumber = Math.floor(Math.random()*4);
	    this.Shoot = 0;
	    	    	
		switch (randomNumber) {
			case 0:
				self.enemy = new Enemy31(self.context, self.image[4]);		
				self.activEnemyPosition = self.enemy.initEnemy(self.Shoot);				
				var d = new Date();
				this.initDateEnemy = d.getTime();
				break;
			case 1:
				self.enemy = new Enemy32(self.context, self.image[2]);
				self.activEnemyPosition = self.enemy.initEnemy(self.Shoot);
				var d = new Date();
				this.initDateEnemy = d.getTime();
				break;
			case 2:
				self.enemy = new Enemy33(self.context, self.image[6]);
				self.activEnemyPosition = self.enemy.initEnemy(self.Shoot);
				var d = new Date();
				this.initDateEnemy = d.getTime();
				break;
			case 3:
				self.enemy = new Enemy34(self.context, self.image[0]);
				self.activEnemyPosition = self.enemy.initEnemy(self.Shoot);
				var d = new Date();
				this.initDateEnemy = d.getTime();
				break;
			default:
				break;
			};	
	},
	
	initEnemyLevel: function() {
	
		var self = this;
		var shootFromHelicopter = 2000;
		self.generateRandomEnemies(self);
		
        this.chooseEnemyInterval = setInterval(function() { 
        	var d = new Date();
        	this.nowDate = d.getTime();
        	
        	if(self.playerHealth >= 10){
        		self.playerDead = true; // player dead 
        	};
        	
        	if(self.playerHealth < 10){
        		//init heath bar
        		self.healthBar.initHealthBar(self.playerHealth);	
        	
        		
        		if(self.Shoot == 0){
        			//enemy shoot us
		        	if( (this.nowDate - self.initDateEnemy >= self.firstShootFromEnemy) && 
			        		(this.nowDate - self.initDateEnemy <= self.firstShootFromEnemy+100) ){
		        		
		        		self.enemy.drawShooterEnemy(); 
	                    self.enemyShotAudio.load();
	                    self.enemyShotAudio.play();
		                        
		        		if (self.randomShoot()){
		                     self.playerHealth ++;
		        		}
		        	};
	        	
		        	if((this.nowDate - self.initDateEnemy) >= (self.firstShootFromEnemy+200) && 
			        	(this.nowDate - self.initDateEnemy) <= (self.firstShootFromEnemy+300)){
		        			self.enemy.drawWalkEnemy();
		        	};
	        	
		        	//enemy shoot us
		        	if( (this.nowDate - self.initDateEnemy) >= (self.secondShootFromEnemy ) && 
			        	(this.nowDate - self.initDateEnemy) <= (self.secondShootFromEnemy + 100)){
		        			self.enemy.drawShooterEnemy();
		        			self.enemyShotAudio.load();
		                    self.enemyShotAudio.play();
		                    
			        		if (self.randomShoot()){
			                      self.playerHealth ++;            
			        		}
		        	};
		        	
		        	if( (this.nowDate - self.initDateEnemy) >= (self.secondShootFromEnemy + 200) && 
			        	(this.nowDate - self.initDateEnemy) <= (self.secondShootFromEnemy + 300)){	        		
		        		self.enemy.drawWalkEnemy();
		        	};
        		};
        		
	        	if((this.nowDate - self.initDateEnemy) >= (self.secondShootFromEnemy + 1000) && 
		        		(this.nowDate - self.initDateEnemy) <= (self.secondShootFromEnemy + 1100)){    		
	        		self.context.clearRect(120, 0, 860, 520);
	        		self.generateRandomEnemies(self);
	        	};	        	
	        
        		if((this.nowDate - self.deadEnemyDate) >= 650 && (this.nowDate - self.deadEnemyDate <= 750)){
            		self.generateRandomEnemies(self);
            	}

	        	
	        	// init helicopter
	        	if((this.nowDate - self.startLevel3 ) >= self.initHelicopter && 
	        		(this.nowDate - self.startLevel3 <= (self.initHelicopter+100))){ 
	        		
	        		self.helicopter = new Helicopter(self.context, self.image[19], self.image[20]);
					self.activHelicopterPosition = self.helicopter.initEnemy(self.shootToHelicopter);
					var d = new Date();
					self.initDateHel = d.getTime();
					
	        	};
	        	
	        	if(!(self.helicopter == undefined) ){
	        		self.count++;

	        		if(self.count === 1){   // do thing in 200ms
		        			if(!self.deadHelicopter){   // if the helicopter is still alive
		        				
		        				//draw new stay in helicoter 
			        			self.activHelicopterPosition = self.helicopter.initEnemy(self.shootToHelicopter);
			        			self.count = 0;
			        			
			        			// draw shoot helicopter
			        			if((this.nowDate - self.initDateHel) >= ( shootFromHelicopter*self.countShootH) && 
			        		        	(this.nowDate - self.initDateHel) <= ( shootFromHelicopter *self.countShootH + 100)){
			        	        		self.helicopter.drawShooterEnemy(self.shootToHelicopter);
			        	        		self.countShootH++;
			        	        		self.enemyShotAudio.load();
			                            self.enemyShotAudio.play();
			                            
			                            if (self.randomShoot()){
			        	                      self.playerHealth +=2;            
			        	        		}
			                            
			        	        	};
		        			
		        			} else{
			        			 //init dead helicopter
		        				self.helicopter.drawDeadEnemy(self.frameDeadHelicopter);
		        				self.flagStartDeadAnimate = true;		
		        			};
				        	
	        		}//  do thing in 200ms
	        		
	        	}// init the helicopter
	        	
        	};// condition if player is still alive
		}, self.interval);
    
	},
	
	checkedPosition: function(mouseCoordinate,theCount) {
		var self = this;
		this.mouseCoordinateX = mouseCoordinate[0];
		this.mouseCoordinateY = mouseCoordinate[1];
		
		// enemy checked the position
        if(this.mouseCoordinateX > self.activEnemyPosition[0] && this.mouseCoordinateX < self.activEnemyPosition[2] &&
        	this.mouseCoordinateY > self.activEnemyPosition[1] && this.mouseCoordinateY < self.activEnemyPosition[3] ){
        	
        	this.Shoot++;
 
			self.enemy.dyingEnemy(self.Shoot);

			if(theCount <= 1){    
                clearInterval(self.chooseEnemyInterval);  
            };
			
         };
      
	      if (this.Shoot === 1){
	  		this.deadEnemy = true; 
	  		var d = new Date();
	  		this.deadEnemyDate = d.getTime();
	  	   } else{
	  		this.deadEnemy = false;
	  	   };
	  	   
	  	  // helicopter checked the position
	  	  
	  	 if(!(self.helicopter == undefined)){
	  		if(!this.deadHelicopter){
	  		 if(this.mouseCoordinateX > self.activHelicopterPosition[0] && this.mouseCoordinateX < self.activHelicopterPosition[2] &&
	 	         	this.mouseCoordinateY > self.activHelicopterPosition[1] && this.mouseCoordinateY < self.activHelicopterPosition[3] ){
	 	         	
	 	         	this.shootToHelicopter ++;
	 	 			if(this.shootToHelicopter >= 21){
	 	 				//dead helicopter
	 	 				this.deadHelicopter = true;
	 	 			}
	 	 			console.log(this.deadHelicopter);
	 	          };
	  		}
	  	 }
	  	
	  	   
	}
})